import Editor from "../editor"
import { SelectionType } from "../models"
import { RenderView } from "../view"
export class KeyboardEvent {
    container: HTMLElement
    editor: any
    view: RenderView
    constructor(editor: Editor,container: HTMLElement,view:RenderView){
        this.editor = editor
        this.container = container
        this.view = view
    }
    beforeInputHandler_(e:InputEvent){
        const inputType = e.inputType
        e.preventDefault()
        console.log(inputType)
        switch(inputType){
            case 'insertText':
                const text = e.data
                if(text)this.editor.insertTextAtCursor(text)
                break;
            case 'insertParagraph':
                this.editor.insertTextAtCursor('\n')
                break;
            case 'deleteContentBackward':
                this.editor.deleteTextAtCursor()
                break;
        }
    }
    selectionchangeHandler_(selection: any){
        if(selection)this.editor.setSelection(selection.anchor,selection.focus)
    }
    addListeners(){
        this.container.addEventListener('beforeinput',this.beforeInputHandler_.bind(this))
        this.view.emitter.on('selection-change', this.selectionchangeHandler_.bind(this))
    }
}
export default KeyboardEvent